package jp.aboutsoft.felicathon.dummy;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;



public class TankaViewMain extends Activity {
	TankaView mView;
    private android.os.Handler handler = new android.os.Handler();
	Timer timer = null;
	
	protected void onPause() {
        super.onPause();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
	}	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 画面情報取得
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        display.getMetrics(displayMetrics);
//        displayXdpi = (int) displayMetrics.xdpi;

//        widthPixels = displayMetrics.widthPixels;
        // ビュー生成
        mView = new TankaView(getApplicationContext(),
                displayMetrics.widthPixels, 
                displayMetrics.heightPixels);
        // View
        setContentView(mView);
		Intent i = getIntent();
//		if (Intent.ACTION_VIEW.equals(i.getAction())) {
			// 外部からインテントとして起動された
			// URI から　ファイルパスに変換
	//	} 

	        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
	        // 設定内容をコミットする
	        sp.edit().commit();

	        final String EDITTEXT_FONT_SIZE = "list_font_size";
	        final String EDITTEXT_FONT_FILE_NORMAL = "edittext_font_file_normal";
	        final String EDITTEXT_FONT_FILE_BOLD = "edittext_font_file_bold";
	        final String EDITTEXT_TIME_MEASURE = "timemeasure_key";

	        String nmff = sp.getString(EDITTEXT_FONT_FILE_NORMAL, "");
	        String bdff = sp.getString(EDITTEXT_FONT_FILE_BOLD, "");
	        File file;


	        String kk1 = sp.getString("1", "");
	        String kk2 = sp.getString("2", "");
	        String kk3 = sp.getString("3", "");
			String sk1 = i.getExtras().getString("4");
			String sk2 = i.getExtras().getString("5");

			Log.v("TankaViewMain","4:"+sk1);
			Log.v("TankaViewMain","5:"+sk2);
			
//			mView.setString("ひさかたの","ひかりのどけき","はるのひに","しづこころなく","はなのちるらむ");
			mView.setString(Hex4ToStr(kk1),Hex4ToStr(kk2),Hex4ToStr(kk3),Hex4ToStr(sk1),Hex4ToStr(sk2));
					
            timer = new Timer(true);
            timer.schedule(new TimerTask() {
                public void run() {
                    handler.post(new Runnable() {
                        public void run() {
                            if (mView.next()) {
                                if (timer != null) {
                                    timer.cancel();
                                    timer = null;
                                }
                            }
                            //                           mView.smoothScrollBy(scrollwidth, 0);
                        }
                    });
                }

            }, 100, 200);

	}

	private String Hex4ToStr(String HEXs){ 
		int Value=0;
		String s = "";
		int x =0;

		while (x < HEXs.length())	 {
			String HEX = HEXs.substring(x,x+4); x += 4;
			char HexDigit[] = HEX.toCharArray();
			Value=(Character.digit(HexDigit[0],16)) * 16 * 16 * 16 +
			(Character.digit(HexDigit[1],16)) * 16 * 16
			+ (Character.digit(HexDigit[2],16)) * 16
			+ (Character.digit(HexDigit[3],16)) ;
			s = s + (char)Value;
		}	

		return s;
	}
}


